local extension = Package("heroes_ev3")
extension.extensionName = "evolution"
--extension.game_modes_whitelist = {"brokenroyal_mode"}

local EV = require "packages/evolution/EV"
local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["heroes_ev3"] = "新约角色09-12",
  ["ev"] = "新约",
}

local kabi = General(extension, "ev__kabi", "ev", 3, 3, General.Agender)

local ev__suction = fk.CreateTriggerSkill{
  name = "ev__suction",
  events = {fk.TargetConfirmed,fk.TargetConfirming},
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.TargetConfirmed then
        return target == player and data.card.type ~= Card.TypeEquip and #player:getPile(self.name) == 0
        and (#data.card.subcards > 0 or not data.card:isVirtual())
      else
        return player:inMyAttackRange(target) and (data.card.trueName =="slash" or data.card.trueName == "poison") and player:getMark("suction-turn") == 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TargetConfirmed then
      return player.room:askForSkillInvoke(player, self.name, nil, "#ev__suction-invoke:::"..data.card:toLogString())
    else
      return player.room:askForSkillInvoke(player, self.name, nil, "#ev__suction-target:::"..data.card:toLogString())
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.TargetConfirmed then
      EV.nullifiedCard(data, data.card, player)
      if player.room:getCardArea(data.card) == Card.Processing or player.room:getCardArea(data.card) == Card.DiscardPile then
        player:addToPile(self.name, data.card, true, self.name)
      end
    else
      player.room:doIndicate(target.id, { player.id })
      TargetGroup:removeTarget(data.targetGroup, target.id)
      TargetGroup:pushTargets(data.targetGroup, player.id)
    end
  end,
}

kabi:addSkill(ev__suction)

local release_trigger = fk.CreateTriggerSkill{
  name = "#release_trigger",
  events = {fk.EventPhaseStart},
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and #player:getPile("ev__suction") > 0
  end,
  on_cost = function(self, event, target, player, data)
    local card = player.room:askForCard(player, 1, 1, false, self.name, true, ".|.|.|ev__suction|.|.", "#ev__release-invoke", "ev__suction")
    if #card > 0 then
      self.cost_data = card[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:moveCardTo(self.cost_data, Card.PlayerHand, player, fk.ReasonJustMove, self.name, "", true)
    EV.OpenCards(room, player, self.cost_data, self.name)
    if Fk:getCardById(self.cost_data).trueName == "slash" or Fk:getCardById(self.cost_data).trueName == "poison" then
      room:addCardMark(Fk:getCardById(self.cost_data), "release-inhand")
    end
  end,
}

local ev__release = fk.CreateViewAsSkill{
  name = "ev__release",
  pattern = ".|.|.|.|ev__slash,ev__poison",
  interaction = function()
    local names = {}
    for _, id in ipairs(Self:getCardIds("h")) do
      if Fk:getCardById(id):getMark("release-inhand") > 0 then
        table.insertIfNeed(names, Fk:getCardById(id).name)
      end
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names}
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select):getMark("@@shown-inhand") == 0 and Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand
  end,
  view_as = function(self, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    card:addSubcards(cards)
    return card
  end,
}

ev__release:addRelatedSkill(release_trigger)
kabi:addSkill(ev__release)

Fk:loadTranslationTable{
  ["ev__kabi"] = "卡比",
  ["designer:ev__kabi"] = "时雨不迟",
  ["ev__suction"] = "吸入",
  [":ev__suction"] = "当你成为非装备牌的目标后，若你的角色牌上没有牌，你可将此牌置于角色牌上，令此牌对你无效；回合技，当你攻击范围内的一名角色成为【杀】或【毒】的目标时，你可将此牌转移给你。",
  ["#ev__suction-invoke"] = "你可令%arg对你无效并置于角色牌上",
  ["#ev__suction-target"] = "你可将此%arg转移给你",
  ["ev__release"] = "释放",
  ["#release_trigger"] = "释放",
  [":ev__release"] = "出牌阶段开始时，你可获得角色牌上的一张牌，然后明置此牌；若此牌为【杀】或【毒】，则直至你失去此牌，你可将一张暗置手牌当此牌使用或打出。",
  ["#ev__release-invoke"] = "释放：你可获得角色牌上的一张牌",
}

local Shameimaru = General(extension, "ev__shameimaru", "ev", 4, 4, General.Female)

local ev__fengshen = fk.CreateTriggerSkill{
  name = "ev__fengshen",
  anim_type = "control",
  events = {fk.TargetSpecified, fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local unshown = table.filter(player:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
    local cards = player.room:askForCard(player, 1, 999, true, self.name, true, ".|.|.|.|.|.|" .. table.concat(unshown, ","), "#ev__fengshen-show")
    if #cards > 0 then
      self.cost_data = cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    EV.OpenCards(room, player, self.cost_data, self.name)
    if event == fk.TargetSpecified then
      room:addPlayerMark(player, "fengshen_from-turn", #self.cost_data)
      local to = room:getPlayerById(data.to)
      if player:distanceTo(to) == 1 then
        local n = #table.filter(to:getCardIds(Player.Hand), function(id) return Fk:getCardById(id):getMark("@@shown-inhand") == 0 end)
        n = math.min(n, #self.cost_data)
        local cards = EV.askForCardsChosen(room, player, to, n, n, "h", self.name)
        if #cards > 0 then
          EV.OpenCards(room, to, cards, self.name, player)
        end
      end
    elseif event == fk.TargetConfirmed then
      room:addPlayerMark(player, "fengshen_to-turn", #self.cost_data)
      local to = room:getPlayerById(data.from)
      if not data.card:isVirtual() and not U.canUseCardTo(room, to, player, data.card, true) then
        EV.nullifiedCard(data, data.card, player)
      end
    end
  end,
}

local fengshen_distance = fk.CreateDistanceSkill{
  name = "#ev__fengshen_distance",
  correct_func = function(self, from, to)
    if from:hasSkill("ev__fengshen") and from:getMark("fengshen_from-turn") > 0 then
      return -from:getMark("fengshen_from-turn")
    end
    if to:hasSkill("ev__fengshen") and to:getMark("fengshen_to-turn") > 0 then
      return to:getMark("fengshen_to-turn")
    end
  end,
}
ev__fengshen:addRelatedSkill(fengshen_distance)
Shameimaru:addSkill(ev__fengshen)

local ev__hongye = fk.CreateActiveSkill{
  name = "ev__hongye",
  anim_type = "offensive",
  min_card_num = 1,
  max_card_num = function (self)
    local n = Self.hp
    if Self:getMark("ev__hongye-turn") > 0 then n = Self:getMark("ev__hongye-turn") - 1 end
    return n
  end,
  min_target_num = 1,
  max_target_num = 99,
  can_use = function(self, player)
    return not player:isNude()
  end,
  prompt = "#ev__hongye",
  card_filter = function(self, to_select, selected)
    if Self:getMark("ev__hongye-turn") == 1 then return end
    return Fk:getCardById(to_select).color == Card.Red and not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    if to_select ~= Self.id then
      local target = Fk:currentRoom():getPlayerById(to_select)
      return Self:distanceTo(target) <= #selected_cards and not target:isNude()
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:setPlayerMark(player, "ev__hongye-turn", #effect.cards)
    room:moveCardTo(effect.cards, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, player.id)
    for _, p in ipairs(effect.tos) do
      local id = EV.askForCardChosen(room, player, room:getPlayerById(p), "hes", self.name)
      room:moveCardTo(id, Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, player.id)
    end
  end,
}

Shameimaru:addSkill(ev__hongye)

Fk:loadTranslationTable{
  ["ev__shameimaru"] = "射命丸文",
  ["designer:ev__shameimaru"] = "KINGDOM43",
  ["ev__fengshen"] = "无双风神",
  [":ev__fengshen"] = "当你指定/成为牌的目标后，你可明置任意张手牌，令当前回合内你与其他角色减少/其他角色与你增加等量的距离；然后若你与目标角色的距离为1/不满足此牌的距离合法性检测，你明置其等量手牌/令此牌对你无效。",
  ["#ev__fengshen-show"] = "无双风神：你可明置任意张手牌",
  ["ev__hongye"] = "红叶扇风",
  [":ev__hongye"] = "出牌阶段，你可弃置至多X张红色牌，弃置距离X以内的任意名角色的各一张牌，然后你下次于本回合内以此法弃置自己的牌数需小于本次（X为你的体力值）。",
  ["#ev__hongye"] = "请选择弃置的红色牌和任意名角色",
}

local shixie = General(extension, "ev__shixie", "ev", 4, 4, General.Female)
local ev__yinci = fk.CreateTriggerSkill{
  name = "ev__yinci",
  frequency = Skill.Compulsory,
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and target == player and player.phase == Player.Start and not player:isKongcheng()
    and player:getMark("@@yinci_invalid") == 0
  end,
  on_cost = function(self, event, target, player, data)
    local slash = Fk:cloneCard("ev__slash")
    local targets = table.map(table.filter(player.room.alive_players, function(p) 
    return not EV.isFriend(player, p) and not player:isProhibited(p, slash) end), Util.IdMapper)
    local tar, id = player.room:askForChooseCardAndPlayers(player, targets, 1, 1, ".|.|.|hand", "#ev__yinci-target", self.name, false)
    if #tar > 0 and id then
      slash:addSubcard(id)
      if not player:prohibitUse(slash) then
        self.cost_data = {tar[1], id}
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data[1])
    room:useVirtualCard("ev__slash", self.cost_data[2], player, to, self.name, true)
  end,

  refresh_events = {fk.CardUsing, fk.TurnStart, fk.TurnEnd},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self.name) and target == player then
      if event == fk.CardUsing then
        return data.card.trueName == "slash"
      elseif event == fk.TurnStart then
        return player:getMark("@@yinci_invalid") > 0
      elseif event == fk.TurnEnd then
        return player:getMark("@@yinci_invalid") > 0 and player:getMark("yinci-turn") > 0
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      room:addPlayerMark(player, "@@yinci_invalid")
      room:setPlayerMark(player, "yinci-turn", 0)
    elseif event == fk.TurnStart then
      room:addPlayerMark(player, "yinci-turn")
    elseif event == fk.TurnEnd then
      room:setPlayerMark(player, "@@yinci_invalid", 0)
    end
  end,
}

local yinci_prohibit = fk.CreateProhibitSkill{
  name = "#ev__yinci_prohibit",
  is_prohibited = function(self, from, to, card)
    return to:hasSkill(self) and to.phase == Player.NotActive and to:getMark("@@yinci_invalid") == 0 and card.color == Card.Black
  end,
}

local ev__xiedu = fk.CreateTriggerSkill{
  name = "ev__xiedu",
  anim_type = "offensive",
  events = {fk.AfterCardTargetDeclared, fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      return target == player and player:hasSkill(self) and (data.card.trueName == "slash" or data.card.trueName == "poison") and
      table.find(U.getUseExtraTargets(player.room, data, false), function(id)
        return player:distanceTo(player.room:getPlayerById(id)) == 1
      end)
    else
      return player:hasSkill(self) and target ~= player and #U.getMark(player, "xiedu") > 0 and table.contains(U.getMark(player, "xiedu"), target.id)
      and data.card.trueName == "jink"
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local targets = table.filter(U.getUseExtraTargets(room, data, false), function(id)
        return player:distanceTo(room:getPlayerById(id)) == 1
      end)
      local tos = room:askForChoosePlayers(player, targets, 1, 99, "#ev__xiedu-choose", self.name, true)
      if #tos > 0 then
        self.cost_data = tos
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      table.insert(data.tos, self.cost_data)
      player.room:sendLog{
        type = "#AddTargetsBySkill",
        from = player.id,
        to = self.cost_data,
        arg = self.name,
        arg2 = data.card:toLogString()
      }
    else
      player.room:askForDiscard(target, 1, 1, true, self.name, false)
    end
  end,

  refresh_events = {fk.TargetConfirmed, fk.TurnEnd},
  can_refresh = function(self, event, target, player, data)
    if event == fk.TargetConfirmed then
      return target ~= player and player:hasSkill(self) and data.card.type == Card.TypeBasic and player.phase ~= Player.NotActive
    else
      return target == player and player:hasSkill(self)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.TargetConfirmed then
      local mark = U.getMark(player, "xiedu-turn")
      table.insertIfNeed(mark, target.id)
      player.room:setPlayerMark(player, "xiedu-turn", mark)
    elseif event == fk.TurnEnd then
      local mark = U.getMark(player, "xiedu-turn")
      player.room:setPlayerMark(player, "xiedu", mark)
    end
  end,
}

ev__yinci:addRelatedSkill(yinci_prohibit)
shixie:addSkill(ev__yinci)
shixie:addSkill(ev__xiedu)

Fk:loadTranslationTable{
  ["ev__shixie"] = "狮蝎",
  ["designer:ev__shixie"] = "KINGDOM43",
  ["ev__yinci"] = "隐刺",
  [":ev__yinci"] = "锁定技，准备阶段，你将一张手牌当【杀】对一名非己方角色使用；你于回合外不是黑色牌的合法目标。当你使用【杀】时，此技能失效直至你的下回合结束。",
  ["@@yinci_invalid"] = "隐刺失效",
  ["#ev__yinci_prohibit"] = "隐刺",
  ["#ev__yinci-target"] = "隐刺：请选择一张手牌和【杀】的目标",
  ["ev__xiedu"] = "蝎毒",
  [":ev__xiedu"] = "你使用的【杀】或【毒】可额外选择距离1以内的任意个目标。其他角色使用【闪】时，若其于你的上回合内成为过基础牌的目标，其弃置一张牌。",
  ["#ev__xiedu-choose"] = "蝎毒：请选择距离1以内的任意名角色",

  ["$ev__yinci1"] = "潜伏，开始。。",
  ["$ev__yinci2"] = "伏击，准备。。",
  ["$ev__xiedu1"] = "很痛苦吧。。",
  ["$ev__xiedu2"] = "我也，讨厌，这样做。。",
  ["~ev__shixie"] = "对不起。。都是我没做好。。",
}
local Chen = General(extension, "ev__chen", "ev", 4, 4, General.Female)
local function Canheshi(target)
  for _, skill in ipairs(target.player_skills) do
    if skill:isPlayerSkill(target) and string.find(Fk:translate(":"..skill.name), Fk:translate("cause")) or string.find(Fk:translate(":"..skill.name), Fk:translate("suffer")) then
      return true
    end
  end
  return false
end

local ev__hechi = fk.CreateTriggerSkill{
  name = "ev__hechi",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local players = {}
    for _, p in ipairs(room.alive_players) do
      if EV.isFriend(player, p) and Canheshi(p) and not p:isNude() then
        local card = room:askForCard(p, 1, 1, true, self.name, true, ".", "#heshi-recast")
        if #card > 0 then
          room:recastCard(card, p, self.name)
          table.insert(players, p)
        end
      end
    end
    for _, p in ipairs(players) do
      local use = room:askForUseCard(p, "slash", "slash", "#heshi-slash", true,
      { bypass_distances = false, bypass_times = true })
      if use then
        player:addCardUseHistory("slash", -1)
        room:useCard(use)
      end
    end
  end,
}

local ev__chixiao = fk.CreateTriggerSkill{
  name = "ev__chixiao",
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play and player:getMark("@ev__chixiao") > 0
  end,
  on_cost = function(self, event, target, player, data)
    local n = player:getMark("@ev__chixiao")
    local cards = player.room:askForCard(player, 1, n, true, self.name, true, ".|.|heart,diamond", "#ev__chixiao:::"..n)
    if #cards > 0 then
      self.cost_data = cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = self.cost_data
    local ids = table.filter(cards, function (id) return room:getCardOwner(id) == player end)
    while #ids > 0 do
      local dis = 999
      for _, p in ipairs(room:getOtherPlayers(player)) do
        if not EV.isFriend(player, p) then
          dis = math.min(dis, player:distanceTo(p))
        end
      end
      local targets = table.map(table.filter(room:getOtherPlayers(player), function (p) 
        return player:distanceTo(p) == dis and not EV.isFriend(player, p) end), Util.IdMapper)
      if #targets == 0 then break end
      local to, card = room:askForChooseCardAndPlayers(player, targets, 1, 1, tostring(Exppattern{ id = ids }), "#ev__chixiao-slash", self.name, false, false)
      if #to > 0 and card then
        room:useVirtualCard("ev__slash", card, player, room:getPlayerById(to[1]), self.name, true)
      else
        break
      end
      ids = table.filter(cards, function (id) return room:getCardOwner(id) == player end)
    end
    if player:getMark("@ev__chixiao") > player.hp then
      room:setPlayerMark(player,"@ev__chixiao", 1)
    end
  end,

  refresh_events = {fk.DamageFinished},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self.name) and data.from and data.from == player
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player,"@ev__chixiao", data.damage)
  end,
}

Chen:addSkill(ev__hechi)
Chen:addSkill(ev__chixiao)

Fk:loadTranslationTable{
  ["ev__chen"] = "陈",
  ["designer:ev__chen"] = "KINGDOM43",
  ["ev__hechi"] = "呵斥",
  [":ev__hechi"] = "阶段技（始），你可令技能描述中含有“造成”或“受到”的所有己方角色各选择是否重铸一张牌，然后以此法重铸牌的角色各选择是否使用【杀】。",
  ["#heshi-recast"] = "你可以重铸一张牌",
  ["#heshi-slash"] = "你可以使用【杀】",
  ["cause"] = "造成",
  ["suffer"] = "受到",
  ["ev__chixiao"] = "赤霄",
  [":ev__chixiao"] = "阶段技（终），你可选择至多X张红色牌，依次将其中的一张牌当【杀】对距离最小的一名非己方角色使用，然后若X大于你的体力值，重置X为1（X为你造成过的伤害数）。",
  ["@ev__chixiao"] = "赤霄",
  ["#ev__chixiao"] = "赤霄：你可以选择至多%arg张红色牌",
  ["#ev__chixiao-slash"] = "请选择其中一张牌和【杀】的目标角色",

  ["$ev__hechi"] = "全体人员，做好战斗准备。",
  ["$ev__chixiao1"] = "准备好了吗？那么，一切按照计划进行。",
  ["$ev__chixiao2"] = "罪恶绝不能被容忍，不管用什么办法我都要阻止你们。",
  ["~ev__chen"] = "有序撤退，不要让队伍崩溃！",
}

local Schwarz = General(extension, "ev__schwarz", "ev", 4, 4, General.Female)

local ev__pojia = fk.CreateTriggerSkill{
  name = "ev__pojia",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card.trueName == "slash"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(data.to)
    if to:getEquipment(Card.SubtypeArmor) then
      room:moveCardTo(to:getEquipment(Card.SubtypeArmor), Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, player.id)
    else
      data.disresponsive = true
    end
  end,
}

local ev__zhanshuzhongjie = fk.CreateActiveSkill{
  name = "ev__zhanshuzhongjie",
  anim_type = "offensive",
  frequency = Skill.Limited,
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    local dis = 1
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      dis = math.max(dis, Self:distanceTo(p))
    end
    return #selected == 0 and to_select ~= Self.id and Self:distanceTo(Fk:currentRoom():getPlayerById(to_select)) == dis
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:setPlayerMark(player, "zhanshuzhongjie", effect.tos[1])
    room:abortPlayerArea(target, {"ArmorSlot"})
  end,
}

local zhanshuzhongjie_reset = fk.CreateTriggerSkill{
  name = "#zhanshuzhongjie_reset",
  mute = true,
  refresh_events = {fk.Deathed, fk.CardUsing},
  can_refresh = function(self, event, target, player, data)
    if event == fk.Deathed then
      return player:hasSkill(self) and target ~= player and player:getMark("zhanshuzhongjie") == target.id
    else
      return player:hasSkill(self) and target == player and data.card.trueName == "slash" and player:getMark("zhanshuzhongjie") ~= 0
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.Deathed then
      player.room:setPlayerMark(player, "zhanshuzhongjie", 0)
      player:setSkillUseHistory("ev__zhanshuzhongjie", 0, Player.HistoryGame)
    else
      data.additionalDamage = (data.additionalDamage or 0) + 1
    end
  end,
}

local zhanshuzhongjie_attackrange = fk.CreateAttackRangeSkill{
  name = "#zhanshuzhongjie_attackrange",
  correct_func = function (self, from, to)
    if from:hasSkill("ev__zhanshuzhongjie") and from:getMark("zhanshuzhongjie") ~= 0 then
      return 2
    end
  end,
}

local zhanshuzhongjie_prohibit = fk.CreateProhibitSkill{
  name = "#zhanshuzhongjie_prohibit",
  is_prohibited = function(self, from, to, card)
    return from:hasSkill(self) and card.trueName == "slash" and from:getMark("zhanshuzhongjie") ~= 0 and from:getMark("zhanshuzhongjie") ~= to.id
  end,
}
ev__zhanshuzhongjie:addRelatedSkill(zhanshuzhongjie_attackrange)
ev__zhanshuzhongjie:addRelatedSkill(zhanshuzhongjie_reset)
ev__zhanshuzhongjie:addRelatedSkill(zhanshuzhongjie_prohibit)
Schwarz:addSkill(ev__pojia)
Schwarz:addSkill(ev__zhanshuzhongjie)
Fk:loadTranslationTable{
  ["ev__schwarz"] = "黑",
  ["designer:ev__schwarz"] = "KINGDOM43",
  ["ev__pojia"] = "破甲箭头",
  [":ev__pojia"] = "当你使用【杀】指定一个目标后，若其装备区里：有防具牌，则你弃置之；没有防具牌，则此【杀】不是【闪】的合法目标。",
  ["ev__zhanshuzhongjie"] = "战术终结",
  [":ev__zhanshuzhongjie"] = "限定技，出牌阶段，你可选择距离最大的一名角色，令除其外的角色不是你使用【杀】的合法目标；若如此做，你废除其防具栏，你的攻击范围+2且你使用【杀】造成的伤害+1直至其死亡。其死亡后，你重置此技能。",
  ["#zhanshuzhongjie_prohibit"] = "战术终结",

  ["$ev__pojia1"] = "他们跑不了。",
  ["$ev__pojia2"] = "我看见你了。",
  ["$ev__zhanshuzhongjie1"] = "让他们有来无回。",
  ["$ev__zhanshuzhongjie2"] = "小姐有仁慈之心，我没有。",
  ["~ev__schwarz"] = "博士，走，我会优先确保你的安全。",
}

local Warfarin = General(extension, "ev__warfarin", "ev", 3, 3, General.Female)

local unstable_plasma = fk.CreateActiveSkill{
  name = "unstable_plasma",
  anim_type = "control",
  prompt = "你可弃置一张牌，令一名己方角色回复1点体力",
  target_num = 1,
  card_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.TrueFunc,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and EV.isFriend(Self, Fk:currentRoom():getPlayerById(to_select))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:moveCardTo(effect.cards[1], Card.DiscardPile, nil, fk.ReasonDiscard, self.name, "", true, player.id)
    local card = Fk:getCardById(effect.cards[1])
    if card.suit == Card.Spade and target.hp >= 2 then
      room:addPlayerMark(target, "@@add_damage")
    else
      local n = 1
      if card.suit == Card.Heart and target.hp < 2 then n = 2 end
      room:recover({
        who = target,
        num = n,
        recoverBy = player,
        skillName = self.name
      })
    end
  end,
}

local plasma_buff = fk.CreateTriggerSkill{
  name = "#plasma_buff",
  refresh_events = {fk.DamageCaused, fk.TurnStart, fk.TurnEnd},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self.name, true, true) and target and target:getMark("@@add_damage") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.DamageCaused then
      data.damage = data.damage + 1
    elseif event == fk.TurnStart then
      target.room:addPlayerMark(target, "plasma-turn")
    elseif event == fk.TurnEnd then
      if target:getMark("plasma-turn") > 0 then
        target.room:setPlayerMark(target, "@@add_damage", 0)
      end
    end
  end,
}

local blood_recourse = fk.CreateTriggerSkill{
  name = "blood_recourse",
  events = {fk.Death},
  can_trigger = function(self, event, target, player, data)
    local friends = table.filter(player.room:getOtherPlayers(player), function(p) return EV.isFriend(player, p) end)
    return target ~= player and player:hasSkill(self) and #friends > 0 and not EV.isFriend(player, target)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local friends = table.filter(player.room:getOtherPlayers(player), function(p) return EV.isFriend(player, p) end)
    local to = table.random(friends, 1)
    to[1]:drawCards(1, self.name)
    local cards_id = target:getCardIds{Player.Hand, Player.Equip}
    --room:obtainCard(player.id, cards_id, false, fk.ReasonPrey)
    room:moveCardTo(cards_id, Card.PlayerHand, player, fk.ReasonJustMove, self.name, "", true)
  end,
}

unstable_plasma:addRelatedSkill(plasma_buff)
Warfarin:addSkill(unstable_plasma)
Warfarin:addSkill(blood_recourse)

Fk:loadTranslationTable{
  ["ev__warfarin"] = "华法琳",
  ["designer:ev__warfarin"] = "KINGDOM43",
  ["unstable_plasma"] = "不稳定血浆",
  [":unstable_plasma"] = "阶段技，你可弃置一张牌，令一名己方角色回复1点体力；若此牌为红桃/黑桃牌且其体力值小于2/不小于2，则改为令其回复2点体力/造成的伤害+1直至其下回合结束。",
  ["@@add_damage"] = "增伤",
  ["blood_recourse"] = "血液回收",
  [":blood_recourse"] = "其他角色死亡时，若其不为己方角色，你可随机选择一名其他己方角色并令其摸一张牌，然后你获得死亡角色的所有牌。",

  ["$unstable_plasma1"] = "医疗器械设置完毕。",
  ["$unstable_plasma2"] = "这里有急救箱，请用这个！",
  ["$blood_recourse1"] = "战场是一个只会制造可怕悲剧的地方，希望那些人也能理解到这点。",
  ["$blood_recourse2"] = "优先救助伤员吧，先别管那些逃跑的敌人了。",
  ["~ev__warfarin"] = "对不起，备用药剂也不够了。。",

}

local Gloria = General(extension, "ev__gloria", "ev", 3, 3, General.Female)
local Nightmare = General(extension, "ev__nightmare", "ev", 3, 3, General.Female)
Nightmare.hidden = true
Gloria.reverse = "ev__nightmare"
Nightmare.reverse = "ev__gloria"

local linghunjiqu = fk.CreateTriggerSkill{
  name = "linghunjiqu",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and target == player and data.card and player:usedSkillTimes(self.name) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local friends = table.filter(player.room.alive_players, function(p) return EV.isFriend(player, p) end)
    local tos = player.room:askForChoosePlayers(player, table.map(friends, Util.IdMapper), 1, 2, "#linghunjiqu-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = tos
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(self.cost_data) do
      local to = room:getPlayerById(p)
      if to:isWounded() then
        room:recover{ who = to, num = 1, skillName = self.name }
      end
    end
    if data.card.color == Card.Red then
      player:drawCards(1, self.name)
    end
  end,
}

local biaolirenge = fk.CreateTriggerSkill{
  name = "biaolirenge",
  mute = true,
  events = {fk.TargetSpecified, fk.TargetConfirmed, fk.BeforeTurnOver},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified or event == fk.TargetConfirmed then
      return target == player and player:hasSkill(self) and (data.card.trueName == "slash" or data.card.type == Card.TypeTrick)
    else
      return target == player and player:hasSkill(self)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TargetSpecified or event == fk.TargetConfirmed then
      return player:getMark("@@biaolirenge") > 0 or player.room:askForSkillInvoke(player, self.name)
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified or event == fk.TargetConfirmed then
      room:notifySkillInvoked(player, self.name)
      player:broadcastSkillInvoke(self.name)
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".",
      }
      room:judge(judge)
      if event == fk.TargetConfirmed and judge.card.color == Card.Red then
        EV.nullifiedCard(data, data.card, player)
        room:addPlayerMark(player, "@@biaolirenge")
      elseif event == fk.TargetSpecified and judge.card.color == Card.Black then
        player:turnOver()
      end
    else
      local duputy = player.deputyGeneral == "ev__gloria"
      EV.changeHero(player, "ev__nightmare", duputy)
      return true
    end
  end,

  refresh_events = {fk.TurnStart, fk.TurnEnd},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@biaolirenge") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.TurnStart then
      player.room:addPlayerMark(player, "biaolirenge-turn")
    else
      if player:getMark("biaolirenge-turn") > 0 then
        player.room:setPlayerMark(player, "@@biaolirenge", 0)
      end
    end
  end,
}

local yeyanmoying = fk.CreateViewAsSkill{
  name = "yeyanmoying",
  anim_type = "offensive",
  pattern = "dark_nightmare",
  card_filter = function(self, to_select, selected)
    if #selected > 0 then return false end
    return Fk:getCardById(to_select).color == Card.Black
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then
      return nil
    end
    local c = Fk:cloneCard("dark_nightmare")
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) <= player:getLostHp()
  end,
}

local yeyanmoying_trigger = fk.CreateTriggerSkill{
  name = "#yeyanmoying_trigger",
  mute = true,
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target:getMark("@@yeyanmoying") > 0 and table.contains(U.getMark(player, "yeyanmoying"), target.id)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if data.card.color == Card.Red then
      player.room:askForDiscard(target, 1, 1, false, self.name, false)
    elseif data.card.color == Card.Black then
      player.room:loseHp(target, 1, "yeyanmoying")
    end
  end,

  refresh_events = {fk.CardUseFinished, fk.TurnStart, fk.TurnEnd},
  can_refresh = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      return target == player and player:hasSkill(self) and data.card.skillName == "yeyanmoying"
    else
      return target:getMark("@@yeyanmoying") > 0 and table.contains(U.getMark(player, "yeyanmoying"), target.id)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      for _, id in ipairs(TargetGroup:getRealTargets(data.tos)) do
        local p = room:getPlayerById(id)
        room:addPlayerMark(p, "@@yeyanmoying")
        local mark = U.getMark(player, "yeyanmoying")
        table.insertIfNeed(mark, id)
        room:setPlayerMark(player, "yeyanmoying", mark)
      end
    else
      if event == fk.TurnStart then
        room:addPlayerMark(target, "yeyanmoying-turn")
      else
        if target:getMark("yeyanmoying-turn") > 0 then
          room:setPlayerMark(target, "@@yeyanmoying", 0)
          local mark = U.getMark(player, "yeyanmoying")
          table.removeOne(mark, target.id)
          room:setPlayerMark(player, "yeyanmoying", mark)
        end
      end
    end
  end,
}

local libiaorenge = fk.CreateTriggerSkill{
  name = "libiaorenge",
  anim_type = "control",
  events = {fk.AfterCardUseDeclared},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      data.card.type == Card.TypeTrick and data.card.color == Card.Black
  end,
  on_use = function(self, event, target, player, data)
    data.unoffsetableList = table.map(player.room.alive_players, Util.IdMapper)
  end,
}

local libiaorenge_trigger = fk.CreateTriggerSkill{
  name = "#libiaorenge_trigger",
  mute = true,
  events = {fk.Damaged, fk.BeforeTurnOver},
  can_trigger = function(self, event, target, player, data)
    if event == fk.Damaged then
      if target == player and player:hasSkill(self) then
        return not table.find(player:getCardIds(Player.Hand), function (id) return Fk:getCardById(id).color == Card.Black end)
      end
    else
      return target == player and player:hasSkill(self)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if event == fk.Damaged then
      player.room:notifySkillInvoked(player, self.name)
      player:broadcastSkillInvoke(self.name)
      player:turnOver()
    else
      local duputy = player.deputyGeneral == "ev__nightmare"
      EV.changeHero(player, "ev__gloria", duputy)
      return true
    end
  end,
}
yeyanmoying:addRelatedSkill(yeyanmoying_trigger)
libiaorenge:addRelatedSkill(libiaorenge_trigger)
Gloria:addSkill(linghunjiqu)
Gloria:addSkill(biaolirenge)
Nightmare:addSkill(yeyanmoying)
Nightmare:addSkill(libiaorenge)

Fk:loadTranslationTable{
  ["ev__gloria"] = "葛罗莉亚",
  ["designer:ev__gloria"] = "KINGDOM43",
  ["cv:ev__gloria"] = "张洪",
  ["ev__nightmare"] = "夜魔",
  ["designer:ev__nightmare"] = "KINGDOM43",
  ["cv:ev__nightmare"] = "张洪",

  ["linghunjiqu"] = "灵魂汲取",
  [":linghunjiqu"] = "回合技，当你使用牌造成伤害时，你可选择一至两名己方角色，令其各回复1点体力；然后若此牌为红色牌，你摸一张牌。",
  ["#linghunjiqu-choose"] = "灵魂汲取：你可选择一至两名己方角色回复1点体力",
  ["biaolirenge"] = "表里人格",
  ["@@biaolirenge"] = "可改为须",
  [":biaolirenge"] = "当你指定/成为【杀】或魔法牌的目标后，你可判定：若结果为黑色/红色，你于此牌结算结束后翻面/令此牌对你无效。然后你将“可”改为“须”直至你的下回合结束。",
  ["yeyanmoying"] = "夜魇魔影",
  [":yeyanmoying"] = "阶段技（X），你可将一张黑色牌当【漆黑梦魇】使用，且直至目标角色的下回合结束，其使用红色/黑色牌时，弃置一张牌/失去1点体力（X为你已损失的体力值+1）。",
  ["@@yeyanmoying"] = "夜魇",
  ["libiaorenge"] = "里表人格",
  [":libiaorenge"] = "锁定技，你使用的黑色魔法牌不是【魔法禁令】的合法目标。当你受到伤害后，若你没有黑色手牌，你翻面。",
  ["#libiaorenge_trigger"] = "里表人格",
  
  ["$linghunjiqu"] = "有、有什么东西可以吃吗？",
  ["$yeyanmoying"] = "祝你有个好梦。。。",
  ["$biaolirenge"] = "要不要让我来当你们的对手呢？",
  ["$libiaorenge"] = "还、还要继续向前走吗？",
  ["~ev__gloria"] = "我。。究竟在做什么啊。。",
  ["~ev__nightmare"] = "我。。究竟在做什么啊。。",
}

local Astesia = General(extension, "ev__astesia", "ev", 4, 4, General.Female)

local xingji__zhanxing = fk.CreateTriggerSkill{
  name = "xingji__zhanxing",
  anim_type = "control",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return EV.isFriend(player, target) and player:hasSkill(self.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(4)
    room.logic:trigger("fk.ZhanXing", player, {ids = cards})
    room:askForGuanxing(player, cards, {#cards, #cards}, {0, 0})
    if player.phase == Player.NotActive then
      local n = player:usedSkillTimes(self.name, Player.HistoryRound)
      local discard = room:askForDiscard(player, n, n, true, self.name, true, ".", "#ev__zhanxing:::"..n)
      if #discard == 0 then
        room:handleAddLoseSkills(player, "-xingji__zhanxing")
      end
    end
  end,
}
Fk:addSkill(xingji__zhanxing)

local ev__tianyi = fk.CreateTriggerSkill{
  name = "ev__tianyi",
  mute = true,
  frequency = Skill.Compulsory,
  derived_piles = {"ev__hui"},
  events = {fk.EventAcquireSkill, fk.EventLoseSkill, "fk.ZhanXing"},
  can_trigger = function(self, event, target, player, data)
    if target == player then
      if event == fk.EventAcquireSkill then
        return data.name == self.name
      elseif event == fk.EventLoseSkill then
        return player:hasSkill(self) and player:hasSkill("ev__xinghui") and (data.name == "ev__zhanxing&" or data.name == "xingji__zhanxing")
      else
        return player:hasSkill(self) and #data.ids > 0
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventAcquireSkill then
      room:handleAddLoseSkills(player, "xingji__zhanxing")
      player:broadcastSkillInvoke(self.name, 1)
    elseif event == fk.EventLoseSkill then
      room:handleAddLoseSkills(player, "-ev__xinghui")
      player:broadcastSkillInvoke(self.name, 3)
    else
      player:broadcastSkillInvoke(self.name, 2)
      local ids = data.ids
      local get = U.askforChooseCardsAndChoice(player, ids, {"OK"}, self.name, "请将其中的一张牌置于角色牌上")
      if #get > 0 then
        table.removeOne(ids, get[1])
        player:addToPile("ev__hui", get, true, self.name)
        data.ids = ids
      end
    end
  end,
}

local ev__xinghui = fk.CreateTriggerSkill{
  name = "ev__xinghui",
  anim_type = "control",
  events = {fk.TargetSpecified, fk.TargetConfirmed},
  derived_piles = {"ev__hui"},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and data.card.trueName == "slash" and not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local unshown = table.filter(player:getCardIds(Player.Hand), function(id)
    return Fk:getCardById(id):getMark("@@shown-inhand") == 0 and Fk:getCardById(id).type ~= Card.TypeEquip end)
    local cards = player.room:askForCard(player, 1, 1, true, self.name, true, tostring(Exppattern{ id = unshown }), "#ev__xinghui")
    if #cards > 0 then
      self.cost_data = cards[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    EV.OpenCards(room, player, self.cost_data, self.name)
    local card = Fk:getCardById(self.cost_data)
    if card.type == Card.TypeBasic and #player:getPile("ev__hui") > 0 then
      local cards = room:askForCard(player, 1, 1, false, self.name, false, ".|.|.|ev__hui|.|.", "#ev__xinghui-get", "ev__hui")
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, self.name, "", true)
    elseif card.type == Card.TypeTrick then
      room:notifySkillInvoked(player, "xingji__zhanxing")
      local cards = room:getNCards(4)
      room.logic:trigger("fk.ZhanXing", player, {ids = cards})
      room:askForGuanxing(player, cards, {#cards, #cards}, {0, 0})
      if player.phase == Player.NotActive then
        local n = player:usedSkillTimes(self.name, Player.HistoryRound)
        local discard = room:askForDiscard(player, n, n, true, self.name, true, ".", "#ev__zhanxing:::"..n)
        if #discard == 0 then
          room:handleAddLoseSkills(player, "-xingji__zhanxing")
        end
      end
    end
  end,
}

local xinghui_slash = fk.CreateTargetModSkill{
  name = "#ev__xinghui_targetmod",
  residue_func = function(self, player, skill, scope, card)
    if player:hasSkill("ev__xinghui") and skill.trueName == "slash_skill" and scope == Player.HistoryPhase
    and #player:getPile("ev__hui") > 0 then
      return #player:getPile("ev__hui")
    end
  end,
}
ev__xinghui:addRelatedSkill(xinghui_slash)
Astesia:addSkill(ev__tianyi)
Astesia:addSkill(ev__xinghui)
Astesia:addRelatedSkill("xingji__zhanxing")
Fk:loadTranslationTable{
  ["ev__astesia"] = "星极",
  ["designer:ev__astesia"] = "KINGDOM43",
  ["ev__tianyi"] = "天仪",
  [":ev__tianyi"] = "锁定技，你拥有“占星”；当你失去“占星”后，你失去“星辉”。当你因“占星”而观看牌堆顶的牌时，你将其中的一张牌置于角色牌上，称为“辉”。",
  ["ev__hui"] = "辉",
  ["ev__xinghui"] = "星辉",
  [":ev__xinghui"] = "你使用【杀】的次数上限+X（X为“辉”数）。当你指定或成为【杀】的目标后，你可明置一张基础牌/魔法牌，获得一张“辉”/发动“占星”。",
  ["#ev__xinghui"] = "星辉：你可明置一张基础牌或魔法牌",
  ["#ev__xinghui-get"] = "请选择一张“辉”获得之",
  ["xingji__zhanxing"] = "占星",
  [":xingji__zhanxing"] = Fk:translate(":ev__zhanxing&"),

  ["$ev__tianyi1"] = "让我成为众人的星光吧。",
  ["$ev__tianyi2"] = "群星为我们照亮前路。",
  ["$ev__tianyi3"] = "别慌，星极，像平常那样来就行。",
  ["$ev__xinghui1"] = "游禽翱翔星海，借您羽翼一用！",
  ["$ev__xinghui2"] = "我之剑光，即为星光！",
  ["~ev__astesia"] = "好累。。但是，还不能倒下。。",
}

local guitongwan = General(extension, "ev__guitongwan", "ev", 3)

local xiuluohaisuo = fk.CreateTriggerSkill{
  name = "xiuluohaisuo",
  switch_skill_name = "xiuluohaisuo",
  events = {fk.TargetSpecified, fk.TargetConfirming},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      local room = player.room
      if event == fk.TargetConfirming then
        if player:getSwitchSkillState(self.name, false) == fk.SwitchYang then
          local targets = U.getUseExtraTargets(room, data, true, true)
          table.removeOne(targets, player.id)
          return #targets > 0 or #AimGroup:getAllTargets(data.tos) > 1
        end
      else
        if player:getSwitchSkillState(self.name, false) == fk.SwitchYin then
          local targets = AimGroup:getAllTargets(data.tos)
          table.removeOne(targets, player.id)
          return #targets > 0
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetConfirming then
      return room:askForSkillInvoke(player, self.name, nil, "#xiuluohaisuo:::"..data.card:toLogString())
    else
      local targets = AimGroup:getAllTargets(data.tos)
      table.removeOne(targets, player.id)
      local to = room:askForChoosePlayers(player, targets, 1, 1, "#xiuluohaisuo-choose", self.name, true)
      if #to > 0 then
        self.cost_data = to[1]
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      local to = room:getPlayerById(self.cost_data)
      to:setChainState(true)
    else
      EV.nullifiedCard(data, data.card, player)
    end
  end,
}

local liehunkuangsha = fk.CreateTriggerSkill{
  name = "liehunkuangsha",
  mute = true,
  events = {fk.TurnEnd, fk.DamageCaused, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return player:hasSkill(self.name) and table.find(player.room.alive_players, function (p) return p.chained end)
    else
      return player:hasSkill(self.name) and data.card and data.card.skillName == self.name
      and (event == fk.DamageCaused or player:isWounded())
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      --return U.askForUseVirtualCard(player.room, player, "ghost_assault", nil, self.name)
      return player.room:askForSkillInvoke(player, self.name)
    else
      if event == fk.DamageInflicted then
        return player.room:askForSkillInvoke(target, self.name, nil, "#liehunkuangsha:"..player.id)
      else
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      room:notifySkillInvoked(player, self.name)
      local targets = table.filter(room:getOtherPlayers(player), function (p) return p.chained end)
      room:useVirtualCard("ghost_assault", nil, player, targets, self.name, true)
    else
      if event == fk.DamageInflicted then
        room:recover({
          who = player,
          num = 1,
          recoverBy = player,
          skillName = self.name
        })
        return true
      else
        data.damageType = fk.ThunderDamage
      end
    end
  end,
}

guitongwan:addSkill(xiuluohaisuo)
guitongwan:addSkill(liehunkuangsha)

Fk:loadTranslationTable{
  ["ev__guitongwan"] = "鬼童丸",
  ["designer:ev__guitongwan"] = "次氯酸512",
  ["xiuluohaisuo"] = "修罗骸锁",
  [":xiuluohaisuo"] = "转换技，①当你成为其他角色使用牌的目标后，若有其他角色满足此牌的合法性检测，则你可令之对你无效。②当你使用牌指定目标后，你可令其中除你外的一名目标角色横置。",
  ["#xiuluohaisuo"] = "修罗骸锁：你可令%arg对你无效",
  ["#xiuluohaisuo-choose"] = "修罗骸锁：你可选择一名角色横置",
  ["liehunkuangsha"] = "猎魂狂杀",
  [":liehunkuangsha"] = "回合技（终），你可视为使用【亡灵入侵】，未横置的角色不是此牌的合法目标；当此牌造成伤害时，改为造成雷电伤害，然后受伤角色可令你回复1点体力，防止之。",
  ["#liehunkuangsha"] = "你可以令%src回复1点体力，防止此伤害",
}

local Texas = General(extension, "ev__texas", "ev", 4, 4, General.Female)
local kuaidi_trigger = fk.CreateTriggerSkill{
  name = "#ev__kuaidi-trigger",
  mute = true,
  events = {fk.RoundStart, fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if event == fk.RoundStart then
      return player:hasSkill(self) and player.room:getTag("RoundCount") == 1
    elseif event == fk.AfterCardsMove then
      if player:hasSkill(self) and not player:isKongcheng() and player.phase ~= Player.Draw then
        for _, move in ipairs(data) do
          return move.to == player.id and move.toArea == Card.PlayerHand
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      room:notifySkillInvoked(player, "ev__kuaidi", "drawcard")
      player:broadcastSkillInvoke("ev__kuaidi", 1)
      player:drawCards(2, self.name)
    else
      local ids = {}
      for _, move in ipairs(data) do
        if move.to == player.id and move.toArea == Player.Hand then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
      local cards = room:askForCard(player, 1, 1, true, self.name, false, tostring(Exppattern{ id = ids }), "ev__kuaidi-invoke")
      if #cards > 0 then
        player:addToPile("ev__kuaidi", cards, true, self.name)
      end
    end
  end,
}

local ev__kuaidi = fk.CreateActiveSkill{
  name = "ev__kuaidi",
  mute = true,
  min_card_num = 1,
  max_card_num = 2,
  target_num = 1,
  expand_pile = "ev__kuaidi",
  can_use = function(self, player)
    return #player:getPile("ev__kuaidi") > 0 and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  target_filter = function(self, to_select, selected)
    return #selected == 0
  end,
  card_filter = function(self, to_select, selected)
    return #selected < 2 and Self:getPileNameOfId(to_select) == "ev__kuaidi"
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    room:notifySkillInvoked(player, self.name, "support")
    player:broadcastSkillInvoke(self.name, 2)
    room:moveCardTo(effect.cards, Card.PlayerHand, room:getPlayerById(effect.tos[1]), fk.ReasonGive, self.name, "", true)
  end,
}

local ev__jianyu = fk.CreateActiveSkill{
  name = "ev__jianyu",
  anim_type = "offensive",
  min_card_num = 2,
  min_target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isNude()
  end,
  prompt = "#ev__jianyu",
  card_filter = function(self, to_select, selected)
    return Fk:getCardById(to_select).trueName == "slash" and Fk:getCardById(to_select):getMark("@@shown-inhand") == 0
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    if to_select ~= Self.id then
      local target = Fk:currentRoom():getPlayerById(to_select)
      return Self:inMyAttackRange(target) and #selected < #selected_cards
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    EV.OpenCards(room, player, effect.cards, self.name)
    player:drawCards(#effect.cards, self.name)
    for _, p in ipairs(effect.tos) do
      room:addPlayerMark(room:getPlayerById(p), "@@jianyu-turn")
    end
  end,
}

local jianyu_prohibit = fk.CreateProhibitSkill{
  name = "#ev__jianyu_prohibit",
  prohibit_use = function(self, player, card)
    if player:getMark("@@jianyu-turn") ~= 0 then
      return Fk:currentRoom():getCardArea(card) == Card.PlayerHand
    end
  end,
  prohibit_response = function(self, player, card)
    if player:getMark("@@jianyu-turn") ~= 0 then
      return Fk:currentRoom():getCardArea(card) == Card.PlayerHand
    end
  end,
}

local jianyu__damaged = fk.CreateTriggerSkill{
  name = "#ev__jianyu_damaged",
  refresh_events = {fk.Damaged},
  mute = true,
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill("ev__jianyu", true, true) and target:getMark("@@jianyu-turn") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(target, "@@jianyu-turn", 0)
  end,
}

ev__kuaidi:addRelatedSkill(kuaidi_trigger)
ev__jianyu:addRelatedSkill(jianyu_prohibit)
ev__jianyu:addRelatedSkill(jianyu__damaged)
Texas:addSkill(ev__kuaidi)
Texas:addSkill(ev__jianyu)

Fk:loadTranslationTable{
  ["ev__texas"] = "德克萨斯",
  ["designer:ev__texas"] = "KINGDOM43",
  ["ev__kuaidi"] = "战术快递",
  [":ev__kuaidi"] = "首轮开始时，你摸两张牌。当你于摸牌阶段外获得牌后，你将其中的一张牌置于角色牌上。阶段技，你可将角色牌上的一至两张牌交给一名角色。",
  ["ev__kuaidi-invoke"] = "请将其中的一张牌置于角色牌上",
  ["ev__jianyu"] = "剑雨",
  [":ev__jianyu"] = "阶段技，你可明置至少两张【杀】，然后你摸等量张牌并选择你攻击范围内的至多等量名角色，这些角色不能于本回合内使用或打出手牌直至其受到伤害后。",
  ["#ev__jianyu"] = "剑雨：你可明置至少两张【杀】",
  ["@@jianyu-turn"] = "剑雨",
  ["#ev__jianyu_prohibit"] = "剑雨",
  ["#ev__jianyu_damaged"] = "剑雨",

  ["$ev__kuaidi1"] = "需要我做什么？",
  ["$ev__kuaidi2"] = "这里需要补充物资吗？我可以帮忙。",
  ["$ev__jianyu1"] = "拔刀！",
  ["$ev__jianyu2"] = "斩尽杀绝！",
  ["~ev__texas"] = "我可能低估了这次行动的难度。",
}

local Saria = General(extension, "ev__saria", "ev", 4, 4, General.Female)
local ev__cnhf = fk.CreateTriggerSkill{
  name = "ev__cnhf",
  frequency = Skill.Compulsory,
  anim = "defensive",
  events = {fk.RoundStart, fk.PreCardEffect},
  can_trigger = function(self, event, target, player, data)
    if event == fk.RoundStart then
      return player:hasSkill(self)
    elseif event == fk.PreCardEffect then
      return player:hasSkill(self.name) and player.room:getPlayerById(data.to) == player and data.card.trueName == "slash"
      and data.card.number > 0 and data.card.number < player:getMaxCards()
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if event == fk.RoundStart then
      player.room:addPlayerMark(player, "AddMaxCards", 2)
      if player:getMaxCards() > player.maxHp * 2 then
        player.room:addPlayerMark(player, "MinusMaxCards", player.maxHp)
        player:setSkillUseHistory("ev__gaizhihua", 0, Player.HistoryGame)
      end
    else
      return true
    end
  end,
}

local ev__gaizhihua = fk.CreateTriggerSkill{
  name = "ev__gaizhihua",
  frequency = Skill.Limited,
  mute = true,
  events = {fk.EventPhaseStart, fk.TurnEnd, fk.CardUsing, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.EventPhaseStart then
        return target == player and player.phase == Player.Finish and player:usedSkillTimes(self.name, Player.HistoryGame) == 0 and player.faceup
      elseif event == fk.TurnEnd then
        return target:isWounded() and player:getMark("@@gaizhihua") > 0
      elseif event == fk.CardUsing then
        return target ~= player and player:getMark("@@gaizhihua") > 0 and data.card.trueName == "slash"
      elseif event == fk.DamageInflicted then
        return target ~= player and player:getMark("@@gaizhihua") > 0 and data.card and data.card.type == Card.TypeTrick
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local pattern
    if event == fk.EventPhaseStart then
      pattern = "#gaizhihua-invoke"
    elseif event == fk.TurnEnd then
      pattern = "#gaizhihua-recover::"..target.id
    elseif event == fk.CardUsing then
      pattern = "#gaizhihua-slash::"..target.id
    elseif event == fk.DamageInflicted then
      pattern ="#gaizhihua-damage::"..target.id
    end
    return room:askForSkillInvoke(player, self.name, nil, pattern)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      room:notifySkillInvoked(player, self.name, "big")
      player:broadcastSkillInvoke(self.name, 1)
      player:turnOver()
      room:addPlayerMark(player, "@@gaizhihua")
    elseif event == fk.TurnEnd then
      room:notifySkillInvoked(player, self.name, "support")
      player:broadcastSkillInvoke(self.name, 2)
      room:recover({
        who = target,
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
    elseif event == fk.CardUsing then
      room:notifySkillInvoked(player, self.name, "control")
      player:broadcastSkillInvoke(self.name, 3)
      room:askForDiscard(target, 1, 1, true, self.name, false)
    elseif event == fk.DamageInflicted then
      room:notifySkillInvoked(player, self.name, "offensive")
      player:broadcastSkillInvoke(self.name, 4)
      data.damage = data.damage + 1
    end
  end,

  refresh_events = {fk.TurnedOver},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:getMark("@@gaizhihua") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@gaizhihua", 0)
  end,
}

Saria:addSkill(ev__cnhf)
Saria:addSkill(ev__gaizhihua)

Fk:loadTranslationTable{
  ["ev__saria"] = "塞雷娅",
  ["designer:ev__saria"] = "KINGDOM43",
  ["ev__cnhf"] = "充能护服",
  [":ev__cnhf"] = "锁定技，点数小于你的手牌上限的【杀】对你无效。每轮开始时，你的手牌上限+2；然后若之大于2X，则你令之-X并重置“钙质化”（X为你的体力上限）。",
  ["ev__gaizhihua"] = "钙质化",
  [":ev__gaizhihua"] = "限定技，结束阶段，若你的角色牌正面朝上，你可翻面，然后直至你翻面：一名角色的回合结束时，你可令其回复1点体力；其他角色使用【杀】时/受到魔法牌造成的伤害时，你可令其弃置一张牌/令此伤害+1。",
  ["@@gaizhihua"] = "<font color=\"#EC5800\">钙质化</font>",
  ["#gaizhihua-invoke"] = "钙质化：你可以翻面",
  ["#gaizhihua-recover"] = "钙质化：你可令%dest回复1点体力",
  ["#gaizhihua-slash"] = "钙质化：你可令%dest弃置一张牌",
  ["#gaizhihua-damage"] = "钙质化：你可令%dest受到的伤害+1",

  ["$ev__gaizhihua1"] = "（发动）凝固吧。",
  ["$ev__gaizhihua2"] = "（回复体力）不许放弃。",
  ["$ev__gaizhihua3"] = "（弃牌）这种程度，又能阻止谁？",
  ["$ev__gaizhihua4"] = "（增伤）压制他们。",
  ["~ev__saria"] = "究竟是哪里搞错了……？",
}

local Specter = General(extension, "ev__specter", "ev", 4, 4, General.Female)

local zhangu_trigger = fk.CreateTriggerSkill{
  name = "#ev__zhangu_trigger",
  mute = true,
  events = {fk.AfterCardTargetDeclared, fk.CardUsing, fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if event == fk.AfterCardTargetDeclared then
        return data.card.trueName == "slash" and table.find(U.getUseExtraTargets(player.room, data, false), function(id)
          return player:distanceTo(player.room:getPlayerById(id)) == 1 end)
      elseif event == fk.CardUsing then
        return player:getMark("@@ev__zhangu") > 0 and data.card.trueName == "slash"
      else
        return player:getMark("@@ev__zhangu") > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local targets = table.filter(U.getUseExtraTargets(room, data, false), function(id)
        return player:distanceTo(room:getPlayerById(id)) == 1
      end)
      local tos = room:askForChoosePlayers(player, targets, 1, 99, "#ev__zhangu-choose", self.name, true)
      if #tos > 0 then
        self.cost_data = tos
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      table.insert(data.tos, self.cost_data)
      player:broadcastSkillInvoke("ev__zhangu")
      player.room:sendLog{
        type = "#AddTargetsBySkill",
        from = player.id,
        to = self.cost_data,
        arg = self.name,
        arg2 = data.card:toLogString(),
        toast = true,
      }
    elseif event == fk.CardUsing then
      data.additionalDamage = (data.additionalDamage or 0) + 1
    else
      player.room:recover({
        who = player,
        num = 1 - player.hp,
        recoverBy = player,
        skillName = "ev__zhangu"
      })
    end
  end,

  refresh_events = {fk.TurnedOver},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:getMark("@@ev__zhangu") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@@ev__zhangu", 0)
  end,
}

local ev__zhangu_filter = fk.CreateFilterSkill{
  name = "#ev__zhangu_filter",
  card_filter = function(self, to_select, player)
    return table.contains(player.player_cards[Player.Hand], to_select.id) and player:getMark("@@ev__zhangu") > 0
  end,
  view_as = function(self, to_select)
    return Fk:cloneCard("ev__slash", to_select.suit, to_select.number)
  end,
}

local ev__zhangu = fk.CreateActiveSkill{
  name = "ev__zhangu",
  prompt = "你可以翻面",
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player.faceup
  end,
  on_use = function(self, room, effect)
    room:getPlayerById(effect.from):turnOver()
    room:addPlayerMark(room:getPlayerById(effect.from), "@@ev__zhangu")
  end
}

local ev__zaisheng = fk.CreateTriggerSkill{
  name = "ev__zaisheng",
  events = {fk.EventPhaseStart, fk.TurnedOver},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player:isWounded() then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Finish
      else
        return player.faceup
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local n = player.hp
    local unshown = table.filter(player:getCardIds(Player.Hand), function(id)
    return Fk:getCardById(id):getMark("@@shown-inhand") == 0 and Fk:getCardById(id).color == Card.Black end)
    local cards = player.room:askForCard(player, n, n, false, self.name, true, tostring(Exppattern{ id = unshown }), "#ev__zaisheng:::"..n)
    --local cards = player.room:askForCard(player, n, n, false, self.name, true, ".|.|spade,club|hand", "#ev__zaisheng:::"..n)
    if #cards > 0 then
      self.cost_data = cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:showCards(self.cost_data)
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = self.name
    })
  end,
}

ev__zhangu:addRelatedSkill(ev__zhangu_filter)
ev__zhangu:addRelatedSkill(zhangu_trigger)
Specter:addSkill(ev__zhangu)
Specter:addSkill(ev__zaisheng)

Fk:loadTranslationTable{
  ["ev__specter"] = "幽灵鲨",
  ["designer:ev__specter"] = "KINGDOM43",

  ["ev__zhangu"] = "肉斩骨断",
  ["@@ev__zhangu"] = "肉斩骨断",
  ["#ev__zhangu_trigger"] = "肉斩骨断",
  [":ev__zhangu"] = "你使用的【杀】可额外选择距离1以内的任意个目标。出牌阶段，若你的角色牌正面朝上，你可翻面，然后直至你翻面：你的手牌均视为【杀】；你使用【杀】造成的伤害+1；当你进入濒死状态时，你将体力回复至1点。",
  ["#ev__zhangu_filter"] = "肉斩骨断",
  ["#ev__zhangu-choose"] = "请选择距离1以内【杀】的额外目标",
  ["ev__zaisheng"] = "深海再生",
  [":ev__zaisheng"] = "结束阶段，或当你翻至正面后，你可展示X张黑色手牌，回复1点体力（X为你的体力值）。",
  ["#ev__zaisheng"] = "你可展示%arg张黑色手牌，回复1点体力",

  ["$ev__zhangu1"] = "不许逃走。",
  ["$ev__zhangu2"] = "谁都。。别想逃走。。",
  ["$ev__zaisheng1"] = "我来自深邃的海洋。",
  ["$ev__zaisheng2"] = "回归深渊吧。",
  ["~ev__specter"] = "必须让他们偿还。。必须。。",
}

return extension